package com.lzp.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @description: 自定义webMvc扩展类
 * @author: lzp
 * @createTime: 2021-10-13 07:28
 **/
@EnableWebMvc
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * 设置viewController视图控制器
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 将路由转发到success
        // 引入thymeleaf，viewName会被thymeleaf捕捉，跳到模板html页面！！
        registry.addViewController("/viewcontroller").setViewName("test1");
    }


    /**
     * 所有webmvc相关配置都会生效，直接配置WebMvcConfigurer
     * @return
     */
    @Bean // 注册组件到容器
    public WebMvcConfigurer webMvcAutoConfigurationAdapter() {
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
            }
        };
        return adapter;
    }


}
